<template>
  <header class="site-header">
    <div class="header-inner">
      <div class="header-logo" @click="goToHome">
        <img src="@/assets/images/logo.png" alt="多多易购" />
      </div>

      <ul class="header-nav">
        <li v-for="(item, index) in links" :key="index">
          <a :href="item.url">{{ item.text }}</a>
        </li>
      </ul>

      <div class="header-search">
        <form action="#" method="get">
          <input type="text" placeholder="搜索商品" />
          <button type="submit">搜索</button>
        </form>
      </div>

      <p v-if="!isLogin">
        <router-link to="/login" @click="login" class="login-link">登录</router-link>
      </p>
      <p v-else>
        <button class="logout-button" @click.prevent="logout">退出登录</button>
      </p>
    </div>
  </header>
</template>

<script>
import headerData from "@/assets/data/header.json";

export default {
  data() {
    return {
      links: headerData,
      isLogin: false,
    };
  },
  created() {
    // 检查本地存储中是否存在登录信息
    const token = window.sessionStorage.getItem("token");
    if (token) {
      this.isLogin = true;
    }
  },
  methods: {
    goToHome() {
      if (this.$route.path !== "/home") {
        this.$router.push("/home");
      }
    },
    login() {
      this.isLogin = true;
      // 在本地存储中保存登录信息
      window.sessionStorage.setItem("token", "your_token_here");
    },
    logout() {
      this.isLogin = false;
      // 从本地存储中移除登录信息
      window.sessionStorage.removeItem("token");
    },
  },
};
</script>

<style scoped>
.site-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 9999;
  background-color: #fff;
  border-bottom: 1px solid #e0e0e0;
  align-items: center;
}

.header-inner {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  height: 70px;
  padding: 0 20px;
  max-width: 1226px;
  margin: 0 auto;
}

.header-logo img {
  width: 120px;
}

.header-logo img:hover {
  cursor: pointer;
}

.header-nav {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-grow: 1;
  list-style: none;
  padding: 0;
  margin: 0;
}

.header-nav li {
  margin-right: 50px;
}

.header-nav a {
  color: #333;
  font-size: 16px;
  text-decoration: none;
}

.header-nav a:hover {
  color: #ff6700;
}

.header-search form {
  position: relative;
}

.header-search input[type="text"] {
  width: 300px;
  padding: 8px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 16px;
  font-size: 14px;
  outline: none;
}

.header-search button[type="submit"] {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  width: 90px;
  height: 36px;
  padding: 0 10px;
  background-color: #ff6700;
  color: #fff;
  border: none;
  border-radius: 18px;
  font-size: 18px;
  cursor: pointer;
}

.logout-button {
  background-color: #fff;
  color: #ff6700;
  border: 1px solid #ff6700;
  border-radius: 18px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
}

.logout-button:hover {
  background-color: #ff6700;
  color: #fff;
}

.header-search {
  margin-right: 20px;
}

.logout-button {
  margin-left: 20px;
}

.login-link {
  font-size: 16px;
}

.login-link:hover {
  color: #ff6700;
}
</style>
